package com.blb.zycommunity.controller;

import com.blb.zycommunity.entity.ResponseResult;
import com.blb.zycommunity.entity.SysDept;
import com.blb.zycommunity.service.ISysDeptService;
import com.blb.zycommunity.utils.TreeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

import com.blb.zycommunity.service.impl.SysDeptServiceImpl;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@RestController
@CrossOrigin
public class SysDeptController {

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private SysDeptServiceImpl sysDeptService;

    @GetMapping("/dept-child")
    public ResponseResult<List<SysDept>> findDeptWithChildren(){
        List<SysDept> deptWithChildren = deptService.findDeptWithChildren();
        List<SysDept> sysDepts = TreeUtils.deptsToTree(deptWithChildren);
        return ResponseResult.ok(sysDepts);
    }

    //部门集合
    @GetMapping("/all-list")
    public ResponseResult<List<SysDept>> sysByAll(){
        List<SysDept> deptList = sysDeptService.selectAll();
        return ResponseResult.ok(deptList);
    }
    //模糊查询
    @GetMapping("/sys-name")
    public ResponseResult<List<SysDept>>sysByName(@RequestParam(required = false,defaultValue = "") String deptName,
                                                  @RequestParam(required = false,defaultValue = "")Long status){
        List<SysDept> sysDepts = sysDeptService.selectByName(deptName, status);
        return ResponseResult.ok(sysDepts);
    }
    //部门添加
    @PostMapping("/add-list")
    public ResponseResult addUser(@RequestBody SysDept sysDept){
        sysDept.setCreateTime(LocalDateTime.now());
        sysDeptService.save(sysDept);
        return ResponseResult.ok("ok");
    }
    

    //部门修改
    @PutMapping("/update-list")
    public ResponseResult updateSysDept(@RequestBody SysDept sysDept){
        sysDeptService.updateById(sysDept);
        return ResponseResult.ok("ok");
    }
    //部门删除
    @DeleteMapping("/deleteSysDept/{deptId}")
    public ResponseResult deleteById(@PathVariable Long deptId){
         sysDeptService.removeById(deptId);
         return ResponseResult.ok("ok");
    }


}
